package com.home.cca.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.home.cca.R;
import com.home.cca.model.Order;

import java.util.List;
import java.util.function.Consumer;

public class OrderAdapter extends RecyclerView.Adapter<OrderViewHolder> {

    private final Context context;
    private final List<Order> orders;
    private Consumer<Order> iconConsumer;
    private Consumer<Order> nameConsumer;
    private Consumer<Order> dateConsumer;

    public OrderAdapter(Context context, List<Order> orders) {
        this.context = context;
        this.orders = orders;
    }

    @NonNull
    @Override
    public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order, parent, false);
        return new OrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OrderViewHolder holder, int position) {
        Order order = orders.get(position);
        holder.bind(order, v -> iconConsumer.accept(order), v -> nameConsumer.accept(order), v -> dateConsumer.accept(order));
    }

    @Override
    public int getItemCount() {
        return orders.size();
    }

    public void setIconConsumer(Consumer<Order> iconConsumer) {
        this.iconConsumer = iconConsumer;
    }

    public void setNameConsumer(Consumer<Order> nameConsumer) {
        this.nameConsumer = nameConsumer;
    }

    public void setDateConsumer(Consumer<Order> dateConsumer) {
        this.dateConsumer = dateConsumer;
    }
}